package com.lw.spring.javademo.login;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取当前的session对象
        HttpSession session = request.getSession(false); // false表示如果没有session就返回null
        if (session != null) {
            session.invalidate(); // 销毁会话
        }
        Cookie cookie = new Cookie("login", "null");
        cookie.setMaxAge(0 );
        response.addCookie(cookie);
        // 重定向到登录页面
        response.sendRedirect("index.jsp");
    }
}

